/**
 * http://usejsdoc.org/
 */
var url = require('url'),
	httpParam = require('./http_param');
	

/**
 * 路由转换
 * @param req
 * @param res
 * @param pathname 路径名称
 */
exports.router = function(res,req){
	var pathname = decodeURI(url.parse(req.url).pathname);
	httpParam.init(res,req);
	var model = pathname.substring(1,pathname.lastIndexOf('/')),
		//controller = httpParam.GET('c'),
		controller = pathname.substring(pathname.lastIndexOf('/')+1),
		classObj = '';
	if (pathname ===  '/favicon.ico') {//忽略/favicon.ico请求
		return;
	}else if(pathname === '/' || pathname === '/index'){//默认进入首页
		res.render(__dirname +'/'+ 'index.jade',{username:"devintam"});
		return;
	}else {
		classObj = require(__dirname+'/' + model);
		console.log(classObj);
		console.log(controller);
		if(classObj){//require成功时，应用call方法，实现类中的方法调用执行
			classObj.init(res,req);
			try {
				classObj[controller].call();
			} catch (e) {
				console.log(e);
				res.writeHead( 404 , {"Content-Type":"text/plain"});
				res.end('can not find source!');
			}
		}else{//调用不存在的模块时，则默认返回404错误
			res.writeHead( 404 , {"Content-Type":"text/plain"});
			res.end('can not find source!');
		}
	}
};